<template>
  <!-- 标签页面 -->
  <div class="box">
    <div class="body">
      <!-- 搜索框 -->
      <el-form ref="form" class="formone" :model="form" label-width="80px">
        <el-form-item label="标签名称">
          <el-input v-model="form.tagName" />
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="form.state" placeholder="请选择">
            <el-option label="启用" value="1" />
            <el-option label="禁用" value="0" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button @click="clearFn">清除</el-button>
          <el-button type="primary" @click="onSubmit">搜索</el-button>
        </el-form-item>
      </el-form>
      <el-button icon="el-icon-edit" type="success" @click="addLabelFn">
        新增标签
      </el-button>
      <el-alert type="info" :closable="false" class="incoTitle">
        <i class="el-icon-info" style="font-size: 16px" /> 数据一共
        {{ total }} 条
      </el-alert>
      <!-- 列表 -->
      <el-table :data="tableData" style="width: 100%">
        <el-table-column
          type="index"
          label="序号"
          :index="indexFn"
          width="120"
        />
        <el-table-column prop="subjectName" label="所属学科" width="160" />
        <el-table-column prop="tagName" label="标签名称" width="160" />

        <el-table-column prop="username" label="创建者" width="140" />
        <el-table-column prop="addDate" label="创建日期" width="240">
          <template #default="{ row }">
            {{ row.addDate | timefilter('YYYY年MM月DD日') }}
          </template>
        </el-table-column>

        <el-table-column
          prop="state"
          label="状态"
          :formatter="formatterFn"
          width="150"
        />
        <el-table-column prop="" label="操作" width="160">
          <template #default="{ row }">
            <el-button
              v-if="row.state === 1"
              type="text"
              @click="stateFn(row.id, 0)"
            >
              禁用
            </el-button>
            <el-button v-else type="text" @click="stateFn(row.id, 1)">
              启用
            </el-button>
            <el-button
              type="text"
              :disabled="row.state === 1 ? true : false"
              @click="editLabelFn(row.id)"
            >
              修改
            </el-button>
            <el-button
              type="text"
              :disabled="row.state === 1 ? true : false"
              @click="delLabelFn(row.id)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        :current-page="page"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
    <!-- 弹窗  -->
    <div class="pop">
      <el-dialog
        :title="showTitle"
        :visible.sync="dialogVisible"
        :before-close="cancel"
      >
        <el-form
          ref="dataform"
          class="popform"
          :model="dataform"
          label-width="100px"
          :rules="rules"
        >
          <el-form-item label="所属学科">
            <el-select v-model="dataform.subjectID" placeholder="请选择">
              <el-option
                v-for="(item, idx) in subsiml"
                :key="idx"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="标签名称">
            <el-input v-model="dataform.tagName" />
          </el-form-item>
        </el-form>
        <template #footer>
          <el-button @click="cancel">取 消</el-button>
          <el-button type="primary" @click="subaddFn"> 确 定 </el-button>
        </template>
      </el-dialog>
    </div>
  </div>
</template>
<script>
import {
  getLabelApi,
  editStateApi,
  delLabelApi,
  getSubSimlApi,
  addLabelApi,
  getLabelDetailsApi,
  editLabelApi
} from '@/api/label'
export default {
  data() {
    return {
      form: {
        tagName: '',
        state: ''
      },
      tableData: [], // 标签列表
      // disabled: false, // 列表左侧操作按钮状态
      content: '', // 启用or禁用
      // 分页
      total: 0, // 总条数
      page: 1,
      pagesize: 10,
      // 弹窗
      dialogVisible: false, // 弹窗状态
      dataform: {
        tagName: '',
        subjectID: ''
      },
      subsiml: [], // 简单学科数组
      rules: {}
    }
  },
  // 标题
  computed: {
    showTitle() {
      return this.dataform.id ? '修改目录' : '新增目录'
    }
  },
  created() {
    this.getLable()
  },
  methods: {
    async getLable(one, two) {
      const res = await getLabelApi(this.page, this.pagesize, one, two)
      this.tableData = res.data.items
      this.total = res.data.counts
    },

    // 顶部表单清除
    clearFn() {
      this.form = {
        tagName: '',
        state: ''
      }
    },
    // 顶部表单搜索
    onSubmit() {
      if (this.form.state === '') {
        this.getLable()
        return
      }
      this.getLable(this.form.tagName, this.form.state)
    },
    // 新增标签按钮（非弹窗）
    async addLabelFn() {
      this.dialogVisible = true
      const res = await getSubSimlApi()
      this.subsiml = res.data
    },
    // 序号
    indexFn(index) {
      return index + 1 + (this.page - 1) * this.pagesize
    },
    // 状态
    formatterFn(row, column, cellValue) {
      if (cellValue === 1 || cellValue === '1') {
        // this.content = '禁用'
        return '已启用'
      } else if (cellValue === 0 || cellValue === '0') {
        // this.content = '启用'
        return '已禁用'
      }
      return cellValue
    },
    // 启用禁用
    async stateFn(id, state) {
      await editStateApi(id, state)
      this.getLable()
    },
    // 列表右侧操作编辑
    async editLabelFn(id) {
      const res1 = await getLabelDetailsApi(id)
      this.dataform = res1.data
      this.dialogVisible = true
      const res = await getSubSimlApi()
      this.subsiml = res.data
    },
    // 列表右侧操作删除
    async delLabelFn(id) {
      this.$confirm(
        '此操作将永久删除该标签 , 是否继续?，是否接续',
        '温馨提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      )
        .then(async () => {
          await delLabelApi(id)
          this.$message.success('删除成功')
          this.getLable()
        })
        .catch(() => {
          console.log('点击了取消')
        })
    },
    // 弹窗内的取消
    cancel() {
      this.dialogVisible = false
      this.dataform = {
        tagName: '',
        subjectID: ''
      }
      // this.$refs.dataform.resetField()
    },
    // 弹窗内确定按钮(新增标签接口)
    async subaddFn() {
      if (this.dataform.id) {
        // 编辑接口
        await editLabelApi(this.dataform.id, this.dataform)
        this.$message.success('修改成功')
      } else {
        // 添加接口
        const res = await addLabelApi(this.dataform)
        console.log(res)
        this.$message.success('添加成功')
      }
      this.cancel()
      this.getLable()
    },
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`)
      this.pagesize = val
      this.getLable()
    },
    handleCurrentChange(val) {
      this.page = val
      this.getLable()
      // console.log(`当前页: ${val}`)
    }
  }
}
</script>
<style scoped lang="scss">
.box {
  background-color: #f0f2f5;
  overflow: hidden;
}
.body {
  background-color: #fff;
  margin: 0 20px;
  padding: 0 20px;
}
// 表单
.el-form {
  margin: 44px 0px 0 0px;
  background-color: rgb(255, 255, 255);
  width: 100%;
  height: 90px;
  padding-top: 20px;
}
.el-form-item {
  float: left;
}
.formone {
  .el-input {
    width: 220px;
  }
}
.el-select {
  width: 220px;
}
// 列表
.el-table--enable-row-transition {
  margin: 0 10px 0 10px;
  width: 98% !important;
  padding: 20px 20px;
}
// 分页
.el-pagination {
  margin-bottom: 140px;
  height: 60px;
  padding-top: 20px;
  width: 100%;
  padding-left: 760px;
  background-color: #fff;
}
// 新增标签按钮
.el-button--success {
  position: absolute;
  right: 60px;
  top: 110px;
}
// 弹窗

::v-deep {
  .el-dialog {
    width: 460px;
    border-radius: 10px;
    // height: 1000px;
  }
}
::v-deep {
  .el-dialog__header {
    border-top-left-radius: 10px;
    border-top-right-radius: 10px;
    background-color: #409eff;
  }
}
.el-input {
  width: 260px;
}
.el-select {
  margin-right: 10px;
  width: 260px;
}
.el-textarea {
  width: 260px;
}

.popform {
  // background-color: red;
  width: 420px;
  height: 160px;
  margin: 0;
}
</style>
